package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.aliyun.CloudStorage;
import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.component.huawei.Moderation;
import com.cdks.wuxiu.api.model.param.AlbumDeleteParam;
import com.cdks.wuxiu.api.model.param.AlbumGetListParam;
import com.cdks.wuxiu.api.model.param.AlbumUploadParam;
import com.cdks.wuxiu.api.service.AlbumService;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.api.util.SmsUtils;
import com.cdks.wuxiu.common.component.constant.ApiCodeEnum;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserAlbumInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "相册")
@Slf4j
@RestController
@RequestMapping("/album")
public class AlbumController {
    @Resource
    private AlbumService albumService;
    @Resource
    private UserService userService;

    
    @ApiOperation("查询用户相册信息")
    @PostMapping("/getList")
    public ApiResult<Object> getList(@RequestBody @Valid AlbumGetListParam param) {
        //未登录只能查看第一页
        if (param.getPage() > 0)
        {
            if ((param.getUserId() == null) || (param.getToken() == null))
            {
                return ApiResult.failed(ApiCodeEnum.NEED_LOGIN);
            }
        }

        List<UserAlbumInfo> album  = albumService.getPhotosByUser(param.getDestId(), param.getPage());

        return ApiResult.success(album);
    }

    @ApiOperation("保存相册照片")
    @PostMapping("/upload")
    public ApiResult<Object> upload(@RequestBody @Valid AlbumUploadParam param) {
        //先机审，机审不过再人审
        int ret  = Moderation.checkImage("user/" + param.getUserId() + "/" + param.getImageUri());
        if (ret < 0)
        {
            albumService.saveImage(param.getUserId(), param.getImageUri(), Constant.AUDIT_TYPE_PHOTO, Constant.AUDIT_STATUS_WAIT);
            SmsUtils.sendManageNotice("用户相册待审");
    
            return ApiResult.failed(10, "提交成功，请等待审核");
        }
        else
        {
            albumService.saveImage(param.getUserId(), param.getImageUri(), Constant.AUDIT_TYPE_PHOTO, Constant.AUDIT_STATUS_PASS);
        }

        return ApiResult.success();
    }

    @ApiOperation("删除相册照片")
    @PostMapping("/delete")
    public ApiResult<Object> delete(@RequestBody @Valid AlbumDeleteParam param) {
        //用做头像不删除
        if (param.getImageUri().equals(param.getUserInfo().getHeadUri())) {
            return ApiResult.failed(10, "该照片是您的头像，不能删除");
        }

        if (param.getUserInfo().getAuthRole() > 0)
        {
            List<UserAlbumInfo> album  = albumService.getPhotosByUser(param.getUserId(), 0);
            if (album.size() <= 3)
            {
                return ApiResult.failed(10, "相册生活照不能少于三张");
            }
        }

        CloudStorage.deleteUserFile(param.getUserId(), param.getImageUri());
        //删除数据
        albumService.deleteImage(param.getUserId(), param.getImageUri());

        return ApiResult.success();
    }  
}
